/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aptech.BugRS.dao;

import com.aptech.BugRS.enity.StaffDTO;
import com.aptech.BugRS.util.Configure;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author KISS
 */
public class StaffDAO {

    private Configure db = null;
    private Connection con = null;
    private ResultSet rs = null;
    private PreparedStatement pst = null;
    private String lastError = "";

    /**
     * Create a new instance of D_Staff
     */
    public StaffDAO() {
        db = new Configure();
    }

    /**
     * @return list of Staff
     */
    public ArrayList<StaffDTO> ReadByAll() {

        ArrayList<StaffDTO> list = new ArrayList<StaffDTO>();
        con = db.getConnection();
        String sql = "SELECT * FROM Staff";
        try {
            pst = con.prepareStatement(sql);
            rs = pst.executeQuery();
            while (rs.next()) {
                StaffDTO sta = new StaffDTO();
                sta.setStaffID(rs.getInt(1));
                sta.setFirstName(rs.getString(2));
                sta.setLastName(rs.getString(3));
                sta.setAddress(rs.getString(4));
                sta.setEmail(rs.getString(5));
                sta.setPhone(rs.getString(6));
                sta.setUserName(rs.getString(7));
                sta.setPassWord(rs.getString(8));
                sta.setDepartmentID(rs.getInt(9));
                list.add(sta);
            }
        } catch (SQLException ex) {
            setLastError("SQL Error");
        } finally {
            db.closeConnection();
        }
        return list;
    }

    /**
     *
     * @param id
     * @return Staff
     */
    public StaffDTO ReadByID(int id) {
        StaffDTO sta = new StaffDTO();
        try {
            con = db.getConnection();
            String sql = "SELECT * FROM Staff WHERE StaffID = ?";
            pst = con.prepareStatement(sql);
            pst.setInt(1, id);
            rs = pst.executeQuery();
            sta.setStaffID(rs.getInt(1));
            sta.setFirstName(rs.getString(2));
            sta.setLastName(rs.getString(3));
            sta.setAddress(rs.getString(4));
            sta.setEmail(rs.getString(5));
            sta.setPhone(rs.getString(6));
            sta.setUserName(rs.getString(7));
            sta.setPassWord(rs.getString(8));
            sta.setDepartmentID(rs.getInt(9));
        } catch (SQLException ex) {
            setLastError("SQL Error");
        }
        return sta;
    }

    /**
     * Method add new staff
     * @param sta
     * @return boolean
     */
    public boolean Create(StaffDTO sta) {

        ArrayList<StaffDTO> list = ReadByAll();
        /*
         * Check exits in database
         */
        for (StaffDTO st : list) {
            if (st.getStaffID() == st.getStaffID()) {
                setLastError("Staff is exitsed! Add fail!");
                return false;
            }
        }
        try {
            con = db.getConnection();
            String sql = "INSERT INTO Staff VALUES(?,?,?,?,?,?,?)";
            pst = con.prepareStatement(sql);
            pst.setInt(1, sta.getStaffID());
            pst.setString(2, sta.getFirstName());
            pst.setString(3, sta.getLastName());
            pst.setString(4, sta.getAddress());
            pst.setString(5, sta.getEmail());
            pst.setString(6, sta.getPhone());
            pst.setString(7, sta.getUserName());
            pst.setString(8, sta.getPassWord());
            pst.setInt(1, sta.getDepartmentID());
            if (pst.executeUpdate() > 0) {
                setLastError("Add Staff Successfully!");
                db.closeConnection();
                return true;
            }
        } catch (SQLException ex) {
            setLastError("SQL Error!");
            db.closeConnection();
            return false;
        }
        setLastError("Add fail!");
        db.closeConnection();
        return false;
    }

    /**
     * Method Update a Staff
     * @param sta
     * @return boolean
     */
    public boolean Update(StaffDTO sta) {
        try {
            con = db.getConnection();
            String sql = "SELECT * FROM Customer WHERE CustomerID = ?";
            pst = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            pst.setInt(1, sta.getStaffID());
            rs = pst.executeQuery();
            rs.first();
            rs.updateString(2, sta.getFirstName());
            rs.updateString(3, sta.getLastName());
            rs.updateString(4, sta.getAddress());
            rs.updateString(5, sta.getEmail());
            rs.updateString(6, sta.getPhone());
            rs.updateString(7, sta.getUserName());
            rs.updateString(8, sta.getPassWord());
            rs.updateRow();
            db.closeConnection();
            setLastError("Update Staff successfully!");
            return true;
        } catch (SQLException ex) {
            setLastError("SQL Error");
            db.closeConnection();
            return false;
        }
    }

    /**
     *
     * @param staff
     * @return boolean
     */
    public boolean isExits(StaffDTO sta) {
        try {
            String sql = "SELECT * FROM Staff WHERE StaffID = ?";
            con = db.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1, sta.getStaffID());
            rs = pst.executeQuery();
            /**
             * Exits return true
             * Not Exit return false;
             */
            if (rs.next()) {
                return true;
            }

        } catch (SQLException ex) {
            setLastError("SQL Error");
        }
        return false;
    }

    public boolean Delete(StaffDTO sta) {
        return true;
    }

    /**
     * @return the lastError
     */
    public String getLastError() {
        return lastError;
    }

    /**
     * @param lastError the lastError to set
     */
    public void setLastError(String lastError) {
        this.lastError = lastError;
    }
    /**
     * @return list of Customer
     */
}
